// 导入模块化组件
import ChatWindow from './modules/chat/views/ChatWindow.vue'
import Settings from './modules/system/views/Settings.vue'
import ModelManage from './modules/system/views/ModelManage.vue'
import LearnNote from './modules/system/views/LearnNote.vue'
import PromptTemplate from './modules/prompt/views/PromptTemplate.vue'
import PromptTemplateEdit from './modules/prompt/views/PromptTemplateEdit.vue'
import KnowledgeBaseManage from './modules/knowledge/views/KnowledgeBaseManage.vue'
import KnowledgeDocument from './modules/knowledge/views/KnowledgeDocument.vue'
import ImageGeneration from './modules/image/views/ImageGeneration.vue'
import BookEdit from './modules/book/views/BookEdit.vue'
import BookManagement from './modules/book/views/BookManagement.vue'
import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
  history: createWebHistory(),
  routes: [
    {
      path: '/',
      redirect: '/chat'
    },

    {
      path: '/register',
      name: 'Register',
      component: () => import('./modules/auth/views/Register.vue'),
      meta: { hideSidebar: true }
    },
    {
      path: '/chat/:sessionId?',
      name: 'ChatWindow',
      component: ChatWindow
    },
    {
      path: '/chat/history',
      name: 'HistorySessions',
      component: () => import('./modules/chat/views/HistorySessions.vue')
    },
    {
      path: '/prompt',
      component: PromptTemplate
    },
    {
      path: '/prompt/edit/:id',
      component: PromptTemplateEdit
    },
    {
      path: '/prompt/create',
      component: PromptTemplateEdit
    },
    {
      path: '/mcp',
      component: () => import('./modules/system/views/McpManage.vue')
    },
    {
      path: '/learn',
      component: LearnNote
    },
    {
      path: '/system/settings',
      component: Settings
    },
    {
      path: '/settings',
      redirect: '/system/settings'
    },
    {
      path: '/history',
      redirect: '/chat/history'
    },
    {
      path: '/system/model',
      component: ModelManage
    },
    {
      path: '/modelmanage',
      redirect: '/system/model'
    },
    {
      path: '/knowledge',
      component: KnowledgeBaseManage
    },
    {
      path: '/kb',
      redirect: '/knowledge'
    },
    {
      path: '/knowledge/:knowledgeId',
      component: KnowledgeDocument
    },
    {
      path: '/kb/:knowledgeId',
      redirect: '/knowledge/:knowledgeId'
    },
    {
      path: '/knowledge/:knowledgeId/add-document',
      component: () => import('./modules/knowledge/views/AddDocument.vue')
    },
    {
      path: '/image',
      component: ImageGeneration
    },
    {
      path: '/book',
      component: BookManagement
    },
    {
      path: '/books',
      redirect: '/book'
    },
    {
      path: '/book-management',
      redirect: '/book'
    },
    {
      path: '/book/edit/:id?',
      component: BookEdit
    },
    {
      path: '/books/edit/:id?',
      redirect: '/book/edit/:id?'
    },
    {
      path: '/book/create',
      component: BookEdit
    },
    {
      path: '/book/new',
      component: BookEdit
    },
    {
      path: '/book/workspace/:id',
      redirect: '/books/workspace/:id'
    },
    {
      path: '/books/workspace/:id/chapter/:chapterNumber/preview',
      name: 'ChapterPreview',
      component: () => import('./modules/book/views/ChapterPreview.vue')
    },
    {
      path: '/books/workspace/:id/outline/:version',
      name: 'OutlineVersion',
      component: () => import('./modules/book/views/OutlineEditor.vue')
    },
    {
      path: '/books/workspace/:id/outline/edit',
      name: 'OutlineEditor',
      component: () => import('./modules/book/views/OutlineEditor.vue')
    },
    // 角色编辑页面路由
    {
      path: '/books/workspace/:id/roles/:characterId',
      name: 'BookRoleEdit',
      component: () => import('./modules/book/views/BookRoleEdit.vue'),
      props: true,
      meta: {
        title: '角色编辑',
        requiresAuth: true,
        keepAlive: false
      }
    },

    // 角色编辑页面路由（带版本号）
    {
      path: '/books/workspace/:id/roles/:characterId/:version',
      name: 'BookRoleEditVersion',
      component: () => import('./modules/book/views/BookRoleEdit.vue'),
      props: true,
      meta: {
        title: '角色编辑',
        requiresAuth: true,
        keepAlive: false
      }
    },
    {
      path: '/books/workspace/:id/inspiration/:inspirationId?',
      name: 'InspirationEditor',
      component: () => import('./modules/inspiration/views/Inspiration.vue')
    },
    {
      path: '/books/workspace/:id/setting/:settingId?',
      name: 'SettingEditor',
      component: () => import('./modules/book/views/SettingEdit.vue')
    },
    {
      path: '/notifications',
      component: () => import('./modules/shared/views/Notifications.vue')
    },
    {
      path: '/inspirations',
      component: () => import('./modules/inspiration/views/Inspiration.vue')
    },
    {
      path: '/book-analysis',
      component: () => import('./modules/book/views/BookAnalysis.vue')
    }
  ]
})

// 全局前置守卫
router.beforeEach(async (to, _from, next) => {
  console.log(`路由导航: ${_from.path} -> ${to.path}`)
  
  // 所有路由都允许访问，不进行前端权限控制
  // 权限控制由后端接口负责
  next()
})

export default router
